package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.IApArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.nntp.Article;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Gareth Cao
 * @date 2022/6/12 19:37
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements IApArticleService {

    @Autowired
    private ArticleMapper apArticleMapper;

    @Override
    public List<Article> load(ArticleHomeDto dto, Integer type) {
        //dto参数校验，
        if (dto.getSize() == null || dto.getSize() == 0) {
            //防止非法的参数，避免dto为空
            dto.setSize(10);
        }
        if (StringUtils.isEmpty(dto.getTag())) {
            //此时tag为空，设置tag为"__all__",查询所有
            dto.setTag("__all__");
        }
        //时间校验
        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }
        //调用持久层
        List<Article> list = apArticleMapper.load(dto, type);
        return list;
    }


    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    /**
     * 保存和修改文章内容+文章配置+文章信息
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        ApArticle apArticle = new ApArticle();
        //1 判断是否新增，则保存三剑客
        if (dto.getId() == null) {
            //TODO 新增
            //1.1 保存文章表
            BeanUtils.copyProperties(dto, apArticle);
            save(apArticle);
            //1.2 保存文章配置表
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);
            //1.3 保存文章内容表
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);
        } else {
            //2 TODO 不是，则修改文章及修改文章内容
            //1.1 修改文章表
            BeanUtils.copyProperties(dto, apArticle);
            updateById(apArticle);
            //1.2 修改文章内容
            //根据文章id查出文章内容表
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getId()));
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }
        return ResponseResult.okResult(apArticle.getId() != null ? apArticle.getId() : "");
    }
}

